﻿/*
____
 L|ypps Web controls for ASP.NET

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace Typps
{
    using System;
    using System.ComponentModel;
    using System.Drawing.Design;
    using System.Runtime.CompilerServices;
    using System.Web.UI.Design;
    using System.Windows.Forms;

    public class FolderNameBrowser : System.Drawing.Design.UITypeEditor
    {
        #region Methods

        [RefreshProperties(RefreshProperties.All)]
        public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
        {
            if (context == null || provider == null || context.Instance == null)
            {
                return base.EditValue(provider, value);
            }

            FolderBrowserDialog folderDlg = new FolderBrowserDialog();
            folderDlg.Description = string.Format("{0} browser", context.PropertyDescriptor.DisplayName);

            string appRelativeUrl = "~/";
            string appBaseUrl = string.Empty;
            IWebApplication application = (IWebApplication)provider.GetService(typeof(IWebApplication));
            if (application != null)
            {
                IProjectItem projectItemFromUrl = application.GetProjectItemFromUrl(appRelativeUrl);
                if (projectItemFromUrl != null)
                {
                    appBaseUrl = projectItemFromUrl.PhysicalPath;
                    folderDlg.SelectedPath = appBaseUrl;
                }
            }
            folderDlg.RootFolder = Environment.SpecialFolder.MyComputer;
            if (folderDlg.ShowDialog() == DialogResult.OK)
            {
                value = folderDlg.SelectedPath;
            }
            folderDlg.Dispose();
            string selectedPath = value as string;
            if (!string.IsNullOrEmpty(selectedPath))
            {
                int startIndex = selectedPath.IndexOf(appBaseUrl);
                if (startIndex != -1)
                {
                    selectedPath = selectedPath.Replace(appBaseUrl, "~/").Replace("\\", "/");
                    if (!selectedPath.EndsWith("/"))
                        selectedPath += "/";
                    return selectedPath;
                }
            }
            return value;
        }

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context != null && context.Instance != null)
            {
                if (!context.PropertyDescriptor.IsReadOnly)
                {
                    return UITypeEditorEditStyle.Modal;
                }
            }
            return UITypeEditorEditStyle.None;
        }

        #endregion Methods
    }
}